package crm.view.tree.students;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Collection;
import java.util.Comparator;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import crm.model.teacherPC.Manager;
import crm.model.teacherPC.connectList.ConnectStudent;
import crm.model.teacherPC.net.StudentPC;
import crm.view.popup.ConnectPopup;



public class ConnectStudentTreeModel extends DefaultTreeModel implements MouseListener {
	Manager manager;
	Object[] arr;
	JTree tree;
	public ConnectStudentTreeModel(Manager manager, JTree tree) {
		super(new DefaultMutableTreeNode("Students"));
		this.manager=manager;
		this.tree=tree;
		arr = manager.connectingStudent.values().toArray();
		System.out.println(arr.length);
	}
	@Override
	public int getChildCount(Object obj) {
		return arr.length;

	}
	@Override
	public Object getChild(Object parent, int index) {
		if (parent instanceof String){
			return arr[index];
		}else{
			return "";
		}
	}
	public Object getRoot() {
		return "Students";
	}

	public boolean isLeaf(Object obj) {
		return obj instanceof ConnectStudent;
	}

	public void mouseClicked(MouseEvent ev) {
		if (MouseEvent.BUTTON3 == ev.getButton() && tree.getPathForLocation(ev.getX(), ev.getY()) != null){
//			tree.setSelectionRow(tree.getPathForLocation(ev.getX(), ev.getY()).getPathCount());

			TreePath selPath = tree.getPathForLocation(ev.getX(), ev.getY());
			if (selPath != null) {
				tree.setSelectionPath(selPath);
			}
			if (tree.getSelectionPath().getLastPathComponent() instanceof ConnectStudent){
				ConnectStudent student = (ConnectStudent)tree.getSelectionPath().getLastPathComponent();
				ConnectPopup.show(manager, student, tree, ev.getX(), ev.getY());
			}
		}

	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}
